Skip to content

Conversation

staabm
Copy link
Contributor

@staabm staabm commented May 15, 2025

70% faster edge case in vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php --filter testBug10979 - which is currently the slowest AnalyserIntegrationTest in the repo.

Before this PR

➜  phpstan-src git:(regr) ✗ php vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php --filter testBug10979 
PHPUnit 9.6.23 by Sebastian Bergmann and contributors.

Random Seed:   1747292998
Warning:       No code coverage driver available

.                                                                   1 / 1 (100%)

Time: 00:03.944, Memory: 74.00 MB

OK (1 test, 1 assertion)
➜  phpstan-src git:(regr) ✗ php vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php --filter testBug10979

After this PR

➜  phpstan-src git:(regr) ✗ php vendor/bin/phpunit tests/PHPStan/Analyser/AnalyserIntegrationTest.php --filter testBug10979
PHPUnit 9.6.23 by Sebastian Bergmann and contributors.

Random Seed:   1747293054
Warning:       No code coverage driver available

.                                                                   1 / 1 (100%)

Time: 00:01.452, Memory: 74.00 MB

OK (1 test, 1 assertion)

Profile before the PR

grafik

triggered on IntersectionType->getFiniteTypes()

➜  phpstan-src git:(fast) ✗ php -v
PHP 8.3.21 (cli) (built: May  6 2025 13:58:10) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.21, Copyright (c) Zend Technologies

return Strings::match(ltrim($name, '\\'), '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/') !== null;
if (!array_key_exists($name, self::$checked)) {
// from https://stackoverflow.com/questions/3195614/validate-class-method-names-with-regex#comment104531582_12011255
self::$checked[$name] = Strings::match(ltrim($name, '\\'), '/^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*(\\\\[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*)*$/') !== null;
Copy link
Contributor

@mvorisek mvorisek May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How much faster https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/v3.75.0/src/DocBlock/TypeExpression.php#L177-L179 this regex is using native preg_match directly?

Regexes are usually fast.

(...*)* in theory can be very slow compared to (...*+)*.

Also the trim can be easily integrated into the regex (and as the linked PHP CS Fixer already does).

And the main slowdown seems to be comming from Nette. As long as the regex is valid, no need to check for anything else than the return value. This is what PHPStan already checks and check well.

@ondrejmirtes
Copy link
Member

Awesome, thank you!

@ondrejmirtes ondrejmirtes merged commit 6a0ca8c into phpstan:2.1.x May 16, 2025
412 of 418 checks passed
@staabm staabm deleted the fast branch May 16, 2025 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants